package sunwul.hospital.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import sunwul.hospital.client.DictFeignClient;
import sunwul.hospital.common.result.Result;
import sunwul.hospital.model.enums.DictEnum;
import sunwul.hospital.model.model.user.Patient;
import sunwul.hospital.user.mapper.PatientMapper;
import sunwul.hospital.user.service.PatientService;

import java.util.List;

/**
 * @author sunwul
 * @date 2024/8/24 11:42:16
 * @desc
 */
@Service
public class PatientServiceImpl extends
        ServiceImpl<PatientMapper, Patient> implements PatientService {
    @Autowired
    DictFeignClient dictFeignClient;

    //获取就诊人列表
    @Override
    public List<Patient> findAllUserId(Long userId) {
        //根据userid查询所有就诊人信息列表
        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<Patient> patientList = baseMapper.selectList(wrapper);
        //通过远程调用，得到编码对应具体内容，查询数据字典表内容
        //其他参数封装
        patientList.forEach(this::packPatient);
        return patientList;
    }

    @Override
    public Patient getPatientId(Long id) {
        return this.packPatient(baseMapper.selectById(id));
    }

    //Patient对象里面其他参数封装
    private Patient packPatient(Patient patient) {
        System.out.println("组装Patient:" + patient);
        //根据证件类型编码，获取证件类型具体指
        String certificatesTypeString = "";
        if (!StringUtils.isEmpty(patient.getCertificatesType())) {
            Result zjzl1 = dictFeignClient.getName("Zjzl", patient.getCertificatesType());
            certificatesTypeString = (String) zjzl1.getData();//联系人证件
        }
        //联系人证件类型
        String contactsCertificatesTypeString = "";
        if (!StringUtils.isEmpty(patient.getContactsCertificatesType())) {
            Result zjzl2 = dictFeignClient.getName("Zjzl", patient.getContactsCertificatesType());
            contactsCertificatesTypeString = (String) zjzl2.getData();
        }
        //省
        String provinceString = (String) dictFeignClient.getName("Province", patient.getProvinceCode()).getData();
        //市
        String cityString = (String) dictFeignClient.getName("City", patient.getCityCode()).getData();
        //区
        String districtString = (String) dictFeignClient.getName("District", patient.getDistrictCode()).getData();
        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());
        return patient;
    }
}
